package com.brainhealth.web.controller.customer;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.Mechanism;
import com.brainhealth.customer.service.IMechanismService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 机构信息Controller
 * 
 * @author wolong
 * @date 2023-07-26
 */
@RestController
@RequestMapping("/customer/mechanism")
public class MechanismController extends BaseController
{
    @Autowired
    private IMechanismService mechanismService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:list')")
    @GetMapping("/list")
    public TableDataInfo list(Mechanism mechanism)
    {
        startPage();
        List<Mechanism> list = mechanismService.selectMechanismList(mechanism);
        return getDataTable(list);
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:export')")
    @Log(title = "机构信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Mechanism mechanism)
    {
        List<Mechanism> list = mechanismService.selectMechanismList(mechanism);
        ExcelUtil<Mechanism> util = new ExcelUtil<Mechanism>(Mechanism.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:query')")
    @GetMapping(value = "/{mechanismId}")
    public AjaxResult getInfo(@PathVariable("mechanismId") Long mechanismId)
    {
        return success(mechanismService.selectMechanismByMechanismId(mechanismId));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:add')")
    @Log(title = "机构信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mechanism mechanism)
    {
        return toAjax(mechanismService.insertMechanism(mechanism));
    }

    /**
     * 新增机构二维码
     */
//    @PreAuthorize("@ss.hasPermi('customer:mechanism:add')")
//    @Log(title = "机构二维码", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping("/qrcode")
    public AjaxResult qrcode(@RequestBody Map<String, Object> map)
    {
        Long mechanismId = Long.valueOf(map.get("mechanismId").toString());
        String qrcodePath = mechanismService.createQrCode(mechanismId);
        if (!StringUtils.isNullOrEmpty(qrcodePath))
        {
            return AjaxResult.success("成功！", qrcodePath);
        }
        return AjaxResult.error("创建机构二维码失败！");
    }

    @Anonymous
    @PostMapping("/getMechanismTree")
    public AjaxResult getMechanismTree()
    {
       return AjaxResult.success(mechanismService.getMechanismTree());
    }


    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:edit')")
    @Log(title = "机构信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mechanism mechanism)
    {
        return toAjax(mechanismService.updateMechanism(mechanism));
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('customer:mechanism:remove')")
    @Log(title = "机构信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mechanismIds}")
    public AjaxResult remove(@PathVariable Long[] mechanismIds)
    {
        return toAjax(mechanismService.deleteMechanismByMechanismIds(mechanismIds));
    }
}
